package 代码记录.指定公司代码练习记录.华为手撕;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Test1 {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Set<List<Integer>> set = new HashSet<>();
        for(int i = 0;i < nums.length;i++){
            if(nums[i] > 0){
                return res;
            }
            int second = i + 1;
            int third = nums.length - 1;
            int sum = 0;
            while(second < third){
                sum = nums[i] + nums[second] + nums[third];
                if(sum < 0){
                    second++;
                }else if(sum > 0){
                    third--;
                }else if(sum == 0){
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    if(!set.contains(list)) {
                        res.add(list);
                        set.add(list);
                    }
                    third--;
                }
            }
        }
        return res;
    }
}
